<?php

$IS_HEZUO = false;
if ($argv[1] == 'hezuo') {
    $IS_HEZUO = true;
}


#检测WHOIS信息是否正常

require_once dirname(__FILE__) . '/init.php';

$fileRunLockFile = dirname(__FILE__) . '/data/runtime/monitor.dns.lock';
fCheckDir($fileRunLockFile);
if (file_exists($fileRunLockFile)) {
    log::w('monitor is runing!');
    die();
}
file_put_contents($fileRunLockFile, date('Y-m-d H:i:s'));

$arr_installed_subdomains = get_all_installed_site_subdomains();
$arr_installed_maindomains = get_main_domains_by_subdomains($arr_installed_subdomains);
$arr_installed_maindomains = array_unique(array_filter($arr_installed_maindomains));
$allInstall_Maindomains = $arr_installed_maindomains;
$arr_installed_subdomains = array_unique(array_filter($arr_installed_subdomains));
$allInstall_Subdomains = $arr_installed_subdomains;
$arrVpsData = require_once dirname(__FILE__) . '/input/vpslist.config.php';

$TIMESTART = date('Y-m-d H:i:s');

$arrFetchHttpResult = array();
$allInstall_Maindomains_Data = array();

foreach ($allInstall_Subdomains as $itemDomain) {
    $arr = explode('.', $itemDomain);
    $length = count($arr);
    $extk = $length - 1;
    $dok = $length - 2;
    $extv = $arr[$extk];
    if (in_array($extv, array('cc', 'com', 'net', 'org', 'info'))) {
        $allInstall_Maindomains_Data[] = $arr[$dok] . '.' . $arr[$extk];
    }
}

$allInstall_Maindomains_Data = array_unique(array_filter($allInstall_Maindomains_Data));
#var_dump($allInstall_Maindomains_Data);die();

if ($IS_HEZUO) {
    $allInstall_Maindomains_Data = getArrayFromFile(dirname(__FILE__) . '/hezuodomain.txt');
}

foreach ($allInstall_Maindomains_Data as $domain) {
    $timeStart = time();
    $arrWhoisInfo = whois_hichina($domain, true);
    $ip = get_hostip_by_name($domain);
    #过期时间
    $exp_int_time = strtotime($arrWhoisInfo['exp_date']);
    $exp_date = date('Y-m-d', $exp_int_time);
    #剩余天数
    $time_domain = $exp_int_time - time();
    $days_domain = floor($time_domain / 86400);

    #extract($arrWhoisInfo);
    if (!stripos($arrWhoisInfo['dns_server'], 'dnspod')) {
        $arrFetchHttpResult['red'][$domain]['domainInfo'] = $arrWhoisInfo;
        $arrFetchHttpResult['red'][$domain]['costTime'] = time() - $timeStart;
        $arrFetchHttpResult['red'][$domain]['ip'] = $ip;
        $arrFetchHttpResult['red'][$domain]['days_domain'] = $days_domain;
    } else {
        $arrFetchHttpResult['green'][$domain]['domainInfo'] = $arrWhoisInfo;
        $arrFetchHttpResult['green'][$domain]['costTime'] = time() - $timeStart;
        $arrFetchHttpResult['green'][$domain]['ip'] = $ip;
        $arrFetchHttpResult['green'][$domain]['days_domain'] = $days_domain;
    }
}


$HTMLBODY = '';
$HTMLBODY.= '
    <b>WHOIS-DNSSERVER监控统计(总计:' . count($allInstall_Maindomains_Data) . '个,异常:' . count($arrFetchHttpResult['red']) . '个)</b>
	<hr />
	<b>Stat By [' . getLocalIp() . '],统计开始于:' . $TIMESTART . ',结束于' . date('Y-m-d H:i:s') . '</b>
    <table border="1" width="100%" bordercolorlight="#C0C0C0" bordercolordark="#C0C0C0" style="border-collapse: collapse;font-size: 14px;">';
$HTMLBODY.='
	<tr height="35">
    <td width="150">域名&nbsp;</td>
	<td width="100">过期时间</td>
	<td width="100">机器IP</td>
    <td width="150">发生时间&nbsp;</td>
    <td width="100">手工Check&nbsp;</td>
    <td width="100">剩余时间</td>
    <td width="200">DNS</td>
    <td width="100">请求耗时</td>
	</tr>
    ';

foreach ($arrFetchHttpResult['red'] as $domain => $domainInfo) {
    $color = ' color="red"';
    $HTMLBODY.='
	<tr height="35">
    <td><font' . $color . '>' . $domain . '</font></td>
	<td><font' . $color . '>' . date('Y-m-d', strtotime($domainInfo['domainInfo']['exp_date'])) . '</font></td>
	<td>' . $domainInfo['ip'] . '</td>
    <td>' . date('Y-m-d H:i:s') . '</td>
    <td><a href="http://whois.hichina.com/whois/api_whois?host=' . $domain . '" target="_blank">手工检测</a></td>
    <td>' . $domainInfo['days_domain'] . '</td>
    <td>' . $domainInfo['domainInfo']['dns_server'] . '</td>
    <td><font' . $color . '>' . $domainInfo['costTime'] . '秒</font>&nbsp;</td>
	</tr>
    ';
}

/*
foreach ($arrFetchHttpResult['green'] as $domain => $domainInfo) {
    $color = '';
    $HTMLBODY.='
	<tr height="35">
    <td><font' . $color . '>' . $domain . '</font></td>
	<td><font' . $color . '>' . $domainInfo['domainInfo']['exp_date'] . '</font></td>
	<td>' . $domainInfo['ip'] . '</td>
    <td>' . date('Y-m-d H:i:s') . '</td>
    <td><a href="http://whois.hichina.com/whois/api_whois?host=' . $domain . '" target="_blank">手工检测</a></td>
    <td>' . $domainInfo['days_domain'] . '</td>
    <td>' . $domainInfo['domainInfo']['dns_server'] . '</td>
    <td><font' . $color . '>' . $domainInfo['costTime'] . '秒</font>&nbsp;</td>
	</tr>
    ';
}
*/


$HTMLBODY.='</table>';
$rf = dirname(__FILE__) . '/data/http/' . date('Y-m-d') . '/' . date('H') . '_dnsreport.html';
FcheckDir($rf);
file_put_contents($rf, $HTMLBODY);

$html = $HTMLBODY;

if ($IS_HEZUO) {
    post_email_by_sae('合作建站-WHOIS-DNSSERVER监控统计[警告(' . count($arrFetchHttpResult['red']) . '个)][正常(' . count($arrFetchHttpResult['green']) . '个)][' . date('Y-m-d H:i:s') . ']', $html, $arrHezuoUsers);
    post_email_by_smtp('合作建站-WHOIS-DNSSERVER监控统计[警告(' . count($arrFetchHttpResult['red']) . '个)][正常(' . count($arrFetchHttpResult['green']) . '个)]', $html, $arrHezuoUsers);
} else {
    post_email_by_sae('WHOIS-DNSSERVER监控统计[警告(' . count($arrFetchHttpResult['red']) . '个)][正常(' . count($arrFetchHttpResult['green']) . '个)][' . date('Y-m-d H:i:s') . ']', $html, $arrUsers);
    post_email_by_smtp('WHOIS-DNSSERVER监控统计[警告(' . count($arrFetchHttpResult['red']) . '个)][正常(' . count($arrFetchHttpResult['green']) . '个)]', $html, $arrUsers);
}

@unlink($fileRunLockFile);
